<?php

namespace App\Http\Controllers\Backstage\Company;

use App\Ant\Excel\Imports\ArchiveImport;
use App\Ant\Jobs\ArchiveReleaseJob;
use App\Ant\WithUpload;
use App\Http\Controllers\BaseController;
use App\Http\Requests\Backstage\Company\Archive\UploadRequest;
use App\Http\Requests\Backstage\Company\Task\CreateRequest;
use App\Http\Requests\Backstage\Company\Task\UpdateRequest;
use App\Models\Company\Archive;
use App\Models\Company\Task;
use App\Transformers\Company\TaskTransformer;
use App\Transformers\MediaTransformer;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class TaskController extends BaseController
{
    use WithUpload;

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $page_size = get_request_page();
        $articles = Task::orderBy('id', SORT_DESC   )->paginate($page_size);
        return $this->response->paginator($articles, new TaskTransformer());
    }

    /**
     * @param CreateRequest $request
     * @throws \Throwable
     */
    public function store(CreateRequest $request)
    {
        $model = \DB::transaction(function () use ($request) {
            $attributes = $request->validated();
            $attributes['author_id'] = \Auth::user()->id;
            $model = Task::create($attributes);
            Excel::import(new ArchiveImport($model), 'public/' . $model->path);
            return $model;
        });
        return $this->response->noContent();
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $model = Task::find($id);
        Excel::import(new ArchiveImport($model), 'public/' . $model->path);
        return $this->response->item($model, new TaskTransformer());
    }

    /**
     * @param Request $request
     * @param $id
     * @throws \Throwable
     */
    public function update(UpdateRequest $request, $id)
    {
        \DB::transaction(function () use ($request, $id) {
            $attributes = $request->validated();
            $task = Task::find($id);
            $model = $task->update($attributes);
            return $model;
        });
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function upload(UploadRequest $request)
    {
        $file = $this->addUpload($request);
        return $this->response->item($file, new MediaTransformer());
    }

    public function release($id)
    {
        $task = Task::find($id);
        if ($task) {
            $archive = Archive::where('task_id', $task->id)->where('is_state', false)->take($task->num)->get();
            $task->increment('count', $archive->count());
            $ids = $archive->pluck('id')->toArray();
            ArchiveReleaseJob::dispatch($ids)
                ->onConnection('redis')
                ->onQueue('release');
        }
    }
}
